import { extend } from 'marbles/utils';
<% require 'yajl' %>

var StaticConfig = extend({
  APP_NAME: <%= Yajl::Encoder.encode(ENV['APP_NAME'] || '') %>,
  API_SERVER: <%= Yajl::Encoder.encode(ENV['API_SERVER'] || '') %>,
  PATH_PREFIX: <%= Yajl::Encoder.encode(ENV['PATH_PREFIX'] || '') %>,
  INSTALL_CERT: <%= Yajl::Encoder.encode(ENV['INSTALL_CERT'] || '') %>,
  ASSET_PATHS: <%= Yajl::Encoder.encode(%w(
    windows-firefox-cert-1.png
    linux-firefox-cert-1.png
    osx-firefox-cert-1.png
    osx-cert-1.png
    osx-cert-2.png
    osx-cert-3.png
    osx-cert-4.png
    linux-chrome-cert-1.png
    linux-chrome-cert-2.png
    linux-chrome-cert-3.png
    linux-chrome-cert-4.png
    linux-chrome-cert-5.png
    windows-cert-1.png
    windows-cert-2.png
    windows-cert-3.png
    windows-cert-4.png
    windows-cert-5.png
    windows-cert-6.png
    windows-cert-7.png

    github-token-gen.png
    github-token-copy.png

    postgres-logo.png
    redis-logo.png
    mysql-logo.png
    mongodb-logo.png
  ).inject({}) { |memo, name| memo[name] = "/assets/"+ asset_path(name).sub(/\A\.\//, ''); memo }) %>
}, window.DashboardConfig || {});

StaticConfig.PROVIDER_ATTRS = {
	postgres: {
		title: 'PostgreSQL',
		appName: 'postgres',
		img: StaticConfig.ASSET_PATHS['postgres-logo.png'],
		route: {
		  mode: 'provider',
		  serviceName: 'postgres'
		},
		uri: {
		  internalTemplate: 'postgres://{PGUSER}:{PGPASSWORD}@{PGHOST}:5432/{PGDATABASE}',
		  externalTemplate: 'postgres://{PGUSER}:{PGPASSWORD}@{defaultRouteDomain}:{port}/{PGDATABASE}'
		},
		cmd: {
		  name: 'psql',
		  internalTemplate: 'psql "user={PGUSER} password={PGPASSWORD} dbname={PGDATABASE} host={PGHOST} port=5432"',
		  externalTemplate: 'psql "user={PGUSER} password={PGPASSWORD} dbname={PGDATABASE} host={defaultRouteDomain} port={port}"'
		},
		discoverdRouteURIEnvKey: 'DATABASE_URL'
	},
	redis: {
		title: 'Redis',
		appName: 'redis',
		img: StaticConfig.ASSET_PATHS['redis-logo.png'],
		route: {
		  mode: 'resource',
		  appNameFromResource: function (resource) {
		    return resource.env['FLYNN_REDIS'];
		  },
		  serviceNameFromResource: function (resource) {
		    return resource.env['FLYNN_REDIS'];
		  }
		},
		uri: {
		  internalTemplate: 'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}',
		  externalTemplate: 'redis://:{REDIS_PASSWORD}@{defaultRouteDomain}:{port}'
		},
		cmd: {
		  name: 'redis-cli',
		  internalTemplate: 'redis-cli -a {REDIS_PASSWORD} -h {REDIS_HOST} -p {REDIS_PORT}',
		  externalTemplate: 'redis-cli -a {REDIS_PASSWORD} -h {defaultRouteDomain} -p {port}'
		},
		discoverdRouteURIEnvKey: 'REDIS_URL'
	},
	mysql: {
		title: 'MySQL',
		appName: 'mariadb',
		img: StaticConfig.ASSET_PATHS['mysql-logo.png'],
		route: {
		  mode: 'provider',
		  serviceName: 'mariadb'
		},
		uri: {
		  internalTemplate: 'mysql://{MYSQL_USER}:{MYSQL_PWD}@{MYSQL_HOST}:3306/{MYSQL_DATABASE}',
		  externalTemplate: 'mysql://{MYSQL_USER}:{MYSQL_PWD}@{defaultRouteDomain}:{port}/{MYSQL_DATABASE}'
		},
		cmd: {
		  name: 'mysql',
		  internalTemplate: 'mysql --user={MYSQL_USER} --password={MYSQL_PWD} --host={MYSQL_HOST} --port=3306 {MYSQL_DATABASE}',
		  externalTemplate: 'mysql --user={MYSQL_USER} --password={MYSQL_PWD} --host={defaultRouteDomain} --port={port} {MYSQL_DATABASE}'
		},
		discoverdRouteURIEnvKey: 'DATABASE_URL'
	},
	mongodb: {
		title: 'MongoDB',
		appName: 'mongodb',
		img: StaticConfig.ASSET_PATHS['mongodb-logo.png'],
		route: {
		  mode: 'provider',
		  serviceName: 'mongodb'
		},
		uri: {
		  internalTemplate: 'mongodb://{MONGO_USER}:{MONGO_PWD}@{MONGO_HOST}:3306/{MONGO_DATABASE}',
		  externalTemplate: 'mongodb://{MONGO_USER}:{MONGO_PWD}@{defaultRouteDomain}:{port}/{MONGO_DATABASE}'
		},
		cmd: {
		  name: 'mongo',
		  internalTemplate: 'mongo --username={MONGO_USER} --password={MONGO_PWD} --host={MONGO_HOST} --port=3306 {MONGO_DATABASE}',
		  externalTemplate: 'mongo --username={MONGO_USER} --password={MONGO_PWD} --host={defaultRouteDomain} --port={port} {MONGO_DATABASE}'
		},
		discoverdRouteURIEnvKey: 'DATABASE_URL'
	}
};

StaticConfig.PROVIDER_ORDER = ['postgres', 'mysql', 'mongodb', 'redis'];

StaticConfig.expandTemplateStr = function (templateStr, vars) {
  return templateStr.replace(/{([^}]+)}/g, function (m, k) {
    return vars[k];
  });
};

export default StaticConfig;
